package com.yxjx.duoxue.datamodel;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.yxjx.duoxue.utils.UIHelper;

public class DMCourseCategory extends DMObject implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4836377698454729395L;

	private String mStr = "";
	private int mType = -1;
	// 课程分类的图标［新增］,首页的分类图标为网络下载，（查询条件筛选处无图标）
	private String iconUrl = "";
	// 级联的二级分类。
	private ArrayList<DMCourseCategory> subCategories = new ArrayList<DMCourseCategory>();
	
	public DMCourseCategory() {
		
	}
	
	public DMCourseCategory(DMCourseCategory item) {
		if (item == null) {
			return;
		}
		setStr(item.getStr());
		setIconUrl(item.getIconUrl());
		setType(item.getType());
		setSubCategories(new ArrayList<DMCourseCategory>());
	}
	
	public static DMCourseCategory copy(DMCourseCategory item) {
		if (item == null) {
			return null;
		}
		DMCourseCategory ret = new DMCourseCategory();
		ret.setStr(item.getStr());
		ret.setIconUrl(item.getIconUrl());
		ret.setSubCategories(item.getSubCategories());
		ret.setType(item.getType());
		return ret;
	}
	
	public DMCourseCategory(int type, String str) {
		setStr(str);
		setType(type);
	}
	
	/**
	 * get Course Type Description [String]
	 * @return
	 */
	public String getStr() {
		return mStr;
	}
	public void setStr(String mStr) {
		this.mStr = mStr;
	}
	
	/**
	 * get Course Type id [Int]
	 * @return
	 */
	public int getType() {
		return mType;
	}
	public void setType(int type) {
		this.mType = type;
	}

	public String getIconUrl() {
		return iconUrl;
	}

	public void setIconUrl(String iconUrl) {
		this.iconUrl = iconUrl;
	}
	
	/**
	 * 解析首页的课程分类项
	 * @param str
	 * @return
	 */
	public static DMCourseCategory fromIndexCourseType(String str) {
		if (UIHelper.isEmpty(str)) {
			return null;
		}
		try {
			DMCourseCategory ret = new DMCourseCategory();
			JSONObject jo = new JSONObject(str);
			ret.setIconUrl(castString(jo,"typeIcon"));
			ret.setStr(castString(jo,"typeName"));
			ret.setType(castInteger(jo,"id"));
			return ret;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<DMCourseCategory> getSubCategories() {
		return subCategories;
	}

	public void setSubCategories(ArrayList<DMCourseCategory> subCategories) {
		this.subCategories = subCategories;
	}

}
